package com.smilive.start.netty.jdk;

import com.smilive.start.netty.utils.ByteBufferUtil;

import java.nio.ByteBuffer;

/**
 * @program: netty_start
 * @Date: 2021-12-30 17:12
 * @Author: smilive
 * @Description: 其他常用方法
 */
public class ByteBufferOtherMethod {

    public static void main(String[] args) {

        ByteBuffer buffer = ByteBuffer.allocate(10);
        buffer.put(new byte[]{'a','b','c','d','e'});
        buffer.flip();

        //读取5个字节
        buffer.get(new byte[5]);
        ByteBufferUtil.debugAll(buffer);

        //rewind 把position 重置为0,重头读
        buffer.rewind();
        ByteBufferUtil.debugAll(buffer);

        //remark & reset
        // mark 标记一个position位置, mark 将position 重置到mark 的位置
        System.out.println((char) buffer.get());
        buffer.mark();
        System.out.println((char)buffer.get());
        System.out.println((char)buffer.get());
        buffer.reset();
        System.out.println((char)buffer.get());
        System.out.println((char)buffer.get());

    }
}
